//package com.runmi.webchat.aftersale.web.common.controller;
//
//import java.io.BufferedOutputStream;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.util.HashMap;
//import java.util.Map;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.ModelMap;
//import org.springframework.web.bind.annotation.RequestHeader;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.multipart.MultipartFile;
//
//import com.runmi.common.utils.DownLoadFileUtil;
//import com.runmi.common.utils.StringUtil;
//import com.runmi.dbcore.entity.ProductChildNode;
//import com.runmi.dbcore.mapper.ProductChildNodeMapper;
//import com.runmi.scm.web.base.BaseController;
//import com.runmi.scm.util.Constants;
//
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//
//
//@Controller
//@RequestMapping(value = "/common/file")
//public class FileController  extends BaseController{
//
//	@Autowired
//	private ProductChildNodeMapper productChildNodeMapper;
//	
//
//	 @RequestMapping(value = "/upload")
//	 @ResponseBody
//	 public Map<String,Object> upload(
//			 @RequestParam(value = "file", required = false) MultipartFile file, 
//			 HttpServletRequest request,
//			 String fileFlag){
//		 	Map<String,Object> result=new HashMap<String,Object>();
//	       // String path = request.getSession().getServletContext().getRealPath("upload");
//	        String path= Constants.SYSTEM_IMAGE_FILE_PATH; 
//	        String fileName = file.getOriginalFilename();
//	       if(StringUtil.isBlank(fileName)){
//	    	   result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
//	    	   result.put(Constants.RETURN_MESSAGE_KEY, "请选择文件");
//	    	   return result;
//	       }
//	       
//	       if(fileName.indexOf("\\")>-1){
//	    	   String fs[]=fileName.split("\\\\");
//	    	   fileName=fs[fs.length-1];
//	       }
//	       
//	    // 获得文件后缀名
//			String tmpName = fileName.substring(fileName.lastIndexOf(".") + 1,
//					fileName.length());
////	       if(tmpName=="xls"){
////	    	   result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
////	    	   result.put(Constants.RETURN_MESSAGE_KEY, "上传的图片格式错误");
////	    	   return result;
////	       }
////	        String fileName = new Date().getTime()+".jpg";
//	        System.out.println(path);
//	        File targetFile = new File(path);
//	        if(!targetFile.exists() && !targetFile.mkdirs()){
//		    	   result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
//		    	   result.put(Constants.RETURN_MESSAGE_KEY, "创建文件夹:"+path+"失败");
//		    	   
//		    	   return result;
//	        }
//	        
//	       try {
//	    	  String prefix=fileName.substring(fileName.lastIndexOf(".")+1); 
//	    	  
//	    	  if(StringUtil.isNotEmptyStr(fileFlag)){
//	    		  if(fileFlag.equals("1")){
//	    			  if(!prefix.equals("xlsx") && !prefix.equals("xlx")){
//	    				 result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
//	   		    	   	 result.put(Constants.RETURN_MESSAGE_KEY, "只能上传Excel文件");
//	   		    	   	 return result;
//	    			  }
//	    		  }
//	    	  }
//	    	  
//	    	  String outFileName=StringUtil.getUUID() +"." + prefix;
//	    	  String outFile=targetFile + "//"+ outFileName;  //本地测试
//	    	  //String outFile="//data//projects//html//upload//erp/upload//"+ outFileName;  //服务器
//            //获取输出流
//	            OutputStream os=new FileOutputStream(outFile);
//            //获取输入流 CommonsMultipartFile 中可以直接得到文件的流
//
//	            InputStream is=file.getInputStream();
//	            int temp;
//	            //一个一个字节的读取并写入
//	            while((temp=is.read())!=(-1))
//	            {
//	                os.write(temp);
//	            }
//	           os.flush();
//	           os.close();
//	           is.close();
//	           result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_SUCCESS);
//			   result.put(Constants.RETURN_MESSAGE_KEY, "success");
//			    String port=request.getServerPort()==80?"":":" + request.getServerPort();
//			    String url=request.getScheme()  + "://" + request.getServerName() + port +  "" + request.getContextPath() + "/upload/" + outFileName;
//			    result.put("url",url);
//			    result.put("prefix", prefix);
//			    result.put("fileName", fileName);
//	       
//	        } catch (IOException e) {
//	            // TODO Auto-generated catch block
//	            e.printStackTrace();
//	            result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
//				result.put(Constants.RETURN_MESSAGE_KEY, "数据异常");
//	        }
//	        //model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);
//
//	        return result;
//	    }
//	 	@ApiOperation(value = "文件下载", notes = "下载指定文件", tags={ "公共", })
//		@RequestMapping(value = "/download",method = RequestMethod.GET)
//		public void download(
//				HttpServletRequest request,HttpServletResponse response,
//				@RequestHeader(value = "accessTocken") String accessTocken,
//				@ApiParam(value = "下载文件在服务器的文件名") @RequestParam(value = "outFileName")String outFileName,
//				@ApiParam(value = "下载输出的文件名") @RequestParam(value = "fileName")String fileName){
//			String path = request.getSession().getServletContext().getRealPath("upload");
//			String outFile=path + "//"+ outFileName;  //本地测试
//			File targetFile = new File(outFile);
//			 DownLoadFileUtil.downloadFile(request, response, targetFile, fileName);
//		}
//	 	
//		 @RequestMapping(value = "/dfile")
//		 public void   dfile(HttpServletRequest request,HttpServletResponse response,String imageName,Integer id){
//			 String fileUrl=Constants.SYSTEM_IMAGE_FILE_PATH+"/" + id + "/" +imageName;  
//		        try {  
//		            File filePath = new File(fileUrl);  
//		            String fileName=filePath.getName();
//		            String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
//		            if(filePath.exists()){  
//		                //读图片  
//		                FileInputStream inputStream = new FileInputStream(filePath);    
//		                int available = inputStream.available();  
//		                byte[] data = new byte[available];  
//		                inputStream.read(data);    
//		                inputStream.close();    
//		                //写图片  
//		                response.setContentType("multipart/form-data");   
//		               // response.setContentType("image/"+prefix);    
//		                response.setHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode(imageName,"UTF-8"));
//		                response.setCharacterEncoding("UTF-8");  
//		                OutputStream stream = new BufferedOutputStream(response.getOutputStream());    
//		                stream.write(data);    
//		                stream.flush();    
//		                stream.close();    
//		            }  
//		        } catch (Exception e) {  
//		            e.printStackTrace();  
//		        } 
//
//		 }
//		
//}
